package eu.andlabs.tutorial.animatedgifs.views;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;

public class GifMovieView extends View {
	private Movie mMovie;
	private long mMoviestart;

	public GifMovieView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
	}

	public GifMovieView(Context context) {
		super(context);
	}

	public void setGifStream(InputStream stream) {
		if (null == stream) {
			return;
		}
		mMovie = Movie.decodeStream(stream);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.TRANSPARENT);
		super.onDraw(canvas);
		final long now = SystemClock.uptimeMillis();

		if (mMoviestart == 0) {
			mMoviestart = now;
		}

		final int relTime = (int) ((now - mMoviestart) % mMovie.duration());
		mMovie.setTime(relTime);
		mMovie.draw(canvas, 0, 0);
		this.invalidate();
	}
}
